package net.huashitong.ssydt.provider.msg.service;


import net.huashitong.ssydt.exceptions.ServiceException;
import net.huashitong.ssydt.msg.constant.MessageConsts;
import net.huashitong.ssydt.msg.entity.SsMsgMessage;
import net.huashitong.ssydt.msg.entity.SsMsgRemindConfig;
import net.huashitong.ssydt.msg.entity.SsMsgUserMessage;
import net.huashitong.ssydt.msg.entity.vo.*;
import net.huashitong.ssydt.provider.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author <a href="mailto:yhy23456@163.com">huiyang.yu</a>
 * @since 2017.11.14
 */
@Service
@Transactional
public class MsgServiceImpl implements MsgService {

    private SsMsgMessageService ssMsgMessageService;

    private SsMsgUserMessageService ssMsgUserMessageService;

    private SsMsgRemindConfigService ssMsgRemindConfigService;

    @Autowired
    public MsgServiceImpl(SsMsgMessageService ssMsgMessageService,
                          SsMsgUserMessageService ssMsgUserMessageService,
                          SsMsgRemindConfigService ssMsgRemindConfigService) {
        this.ssMsgMessageService = ssMsgMessageService;
        this.ssMsgUserMessageService = ssMsgUserMessageService;
        this.ssMsgRemindConfigService = ssMsgRemindConfigService;
    }

    @Override
    public MessageOutVO getMessages(Long messageId) {
        SsMsgMessage message = ssMsgMessageService.selectByPrimaryKey(messageId);
        if (message == null) {
            throw new ServiceException("消息不存在");
        }
        return new MessageOutVO(message);
    }

    @Override
    public MessageListOutVO getMessagesList(String userId, MessageInVO messageInVO) {
        messageInVO.setUserId(userId);
        synSsMsgUserMessage(messageInVO);
        MessageListOutVO vo = new MessageListOutVO();
        List<MessageOutVO> list = ssMsgMessageService.findUnReadMessage(messageInVO);
        vo.setMessages(list);
        return vo;
    }


    @Override
    public void putMessages(String userId, Long messageId) {
        SsMsgUserMessage ssMsgUserMessage = ssMsgUserMessageService.getByUserIdWithMessageId(userId, messageId);
        if (ssMsgUserMessage != null) {
            ssMsgUserMessage.setIsRead((byte) 1);
            ssMsgUserMessageService.updateByPrimaryKey(ssMsgUserMessage);
        }
    }

    @Override
    public void putMessagesAll(String userId, MessageReadInVO messageReadInVO) {
        if (messageReadInVO.getLastTime() == null) {
            throw new ServiceException("参数错误");
        }
        messageReadInVO.setUserId(userId);
        messageReadInVO.setLastDateTime(new Date(messageReadInVO.getLastTime()));
        ssMsgUserMessageService.updateRead(messageReadInVO);
    }

    @Override
    public RemindConfigVO getRemindConfig(String userId) {
        SsMsgRemindConfig ssMsgRemindConfig = ssMsgRemindConfigService.getByUserId(userId);
        if (ssMsgRemindConfig == null) {
            ssMsgRemindConfig = new SsMsgRemindConfig();
            ssMsgRemindConfig.setUserId(userId);
            ssMsgRemindConfig.setTotalSwitch((byte) 1);
            ssMsgRemindConfig.setCommentSwitch((byte) 1);
            ssMsgRemindConfig.setPraiseSwitch((byte) 1);
            ssMsgRemindConfigService.insert(ssMsgRemindConfig);
        }
        return new RemindConfigVO(ssMsgRemindConfig);
    }

    @Override
    public void putRemindConfig(String userId, RemindConfigVO remindConfigVO) {
        SsMsgRemindConfig ssMsgRemindConfig = ssMsgRemindConfigService.getByUserId(userId);
        if (remindConfigVO.getTotalSwitch() != null) {
            byte totalSwitch = (byte) (remindConfigVO.getTotalSwitch() ? 1 : 0);
            ssMsgRemindConfig.setTotalSwitch(totalSwitch);
        }
        if (remindConfigVO.getCommentSwitch() != null) {
            byte commentSwitch = (byte) (remindConfigVO.getTotalSwitch() ? 1 : 0);
            ssMsgRemindConfig.setCommentSwitch(commentSwitch);
        }
        if (remindConfigVO.getPraiseSwitch() != null) {
            byte praiseSwitch = (byte) (remindConfigVO.getTotalSwitch() ? 1 : 0);
            ssMsgRemindConfig.setPraiseSwitch(praiseSwitch);
        }
        ssMsgRemindConfigService.updateByPrimaryKeySelective(ssMsgRemindConfig);
    }


    /**
     * 同步未拉取消息
     *
     * @param messageInVO
     */
    private void synSsMsgUserMessage(MessageInVO messageInVO) {
        if ((StringUtils.isNotBlank(messageInVO.getMsgType())
                && messageInVO.getMsgType().equals(MessageConsts.MsgType.SYSTEM))
                || StringUtils.isBlank(messageInVO.getMsgType())) {
            if (messageInVO.getLastTime() == null || messageInVO.getLastTime().equals(0L)) {
                messageInVO.setLastTime(System.currentTimeMillis());
            }
            List<SsMsgMessage> ssMsgMessages = ssMsgMessageService.findUnReadGroupSendMessage(messageInVO);
            List<SsMsgUserMessage> ssMsgUserMessages = new ArrayList<>();
            for (SsMsgMessage ssMsgMessage : ssMsgMessages) {
                SsMsgUserMessage ssMsgUserMessage = new SsMsgUserMessage();
                ssMsgUserMessage.setUserId(messageInVO.getUserId());
                ssMsgUserMessage.setMessageId(ssMsgMessage.getId());
                ssMsgUserMessage.setIsDelete((byte) 0);
                ssMsgUserMessage.setIsRead((byte) 0);
                ssMsgUserMessages.add(ssMsgUserMessage);
            }
            if (!ssMsgUserMessages.isEmpty()) {
                ssMsgUserMessageService.batchInsert(ssMsgUserMessages);
            }
        }
    }
}
